package com.cloudwise.dao;

import com.cloudwise.entity.Dept;
import com.cloudwise.entity.DeptVo;
import com.cloudwise.util.MySqlTool;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.awt.print.Pageable;
import java.sql.JDBCType;
import java.util.List;

/**
 * (Dept)表数据库访问层
 *
 * @author
 * @since 2021-10-11 10:49:58
 */
public interface DeptDao {

    /**
     * 通过ID查询单条数据
     *
     * @param deptno 主键
     * @return 实例对象
     */
    @Select("select * from  dept where deptno=#{deptno}")
    @Results(id="OOO" ,value = {
            @Result(property = "deptno",column = "deptno",id=true),
            @Result(property = "emps",column = "deptno",many = @Many(select = "com.cloudwise.dao.EmpDao.queryEmpByDeptno"))
    })
    Dept queryById(Integer deptno);
    /**
     * 查询指定行数据
     *
     * @param dept 查询条件
     * @return 对象列表
     */
    @SelectProvider(type = MySqlTool.class,method = "makeSql")
    List<Dept> queryAllByLimit(Dept dept);

    @Select("select d.*,aaa.avg_sal from (select avg(sal) avg_sal,deptno from emp group by deptno) aaa left join dept d on aaa.deptno=d.deptno")
   @Results(id = "myResult" ,value = {
           @Result(property = "deptno"  ,column = "deptno",id = true,javaType = Integer.class,jdbcType = JdbcType.INTEGER),
           @Result(property = "dname" ,column = "dname"),
           @Result(property = "loc" ,column = "loc"),
           @Result(property = "avgSal" ,column = "avg_sal")
   })
    List<DeptVo> queryAllByAvgSal();


    /**
     * 新增数据
     *
     * @param dept 实例对象
     * @return 影响行数
     */
    @Insert("insert  into dept (dname,loc) values (#{dname},#{loc})")
    int insert(Dept dept);

    /**
     * 修改数据
     *
     * @param dept 实例对象
     * @return 影响行数
     */
    @Update("update dept set loc =#{loc} where deptno=#{deptno}")
    int update(Dept dept);

    /**
     * 通过主键删除数据
     *
     * @param deptno 主键
     * @return 影响行数
     */
    @Delete("delete from dept where deptno=#{deptno}")
    int deleteById(Integer deptno);

}

